const utils = require("./utils");
var DEFAULT_CONTENT_TYPE = {
  "Content-Type": "application/x-www-form-urlencoded",
};
function setContentTypeIfUnset(headers, value) {
  if (
    !utils.isUndefined(headers) &&
    utils.isUndefined(headers["Content-Type"])
  ) {
    headers["Content-Type"] = value;
  }
}
const defaults = {
  adapter: require("./xhr"),
  transformRequest: [
    function transformRequest(data, headers) {
      if (utils.isObject(data)) {
        setContentTypeIfUnset(headers, "application/json");
        return JSON.stingfy(data);
      }
      if (utils.isURLSearchParams(data)) {
        setContentTypeIfUnset(
          headers,
          "application/x-www-form-urlencoded;charset=utf-8"
        );
        return data.toString();
      }
      console.log(data, "data");
      return data;
    },
  ],
  transformResponse: [
    function transformResponse(data) {
      if (typeof data === "string") {
        data = JSON.parse(data);
      }
      return data;
    },
  ],
  timeout: 0,
  xsrfCookieName: "XSRF-TOKEN",
  xsrfHeaderName: "X-XSRF-TOKEN",

  maxContentLength: -1,

  validateStatus: function validateStatus(status) {
    return status >= 200 && status < 300;
  },
};

defaults.headers = {
  common: {
    Accept: "application/json, text/plain, */*",
  },
};
utils.forEach(["post", "put"], function (method) {
  defaults.headers[method] = Object.assign(DEFAULT_CONTENT_TYPE, {});
});
module.exports = defaults;
